/**
 * 
 */
package com.tw.trips.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import com.ximpleware.AutoPilot;
import com.ximpleware.VTDGen;
import com.ximpleware.VTDNav;
import com.ximpleware.XMLModifier;

/**
 * @author M1005264
 *
 */
public class XMLUtil {


	/***************************************************************************
	 * XML Constants
	 **************************************************************************/
	public static final String XML_START_TAG = "<";

	public static final String XML_END_TAG = ">";

	public static final String XML_CLOSE_ENDTAG = "/>";

	public static final String XML_CLOSE_STARTTAG = "</";

	public static final String XML_NEW_LINE = "\n";

	public static final String SPACE_STR = " ";

	public static final String QUOTES_STR = "\"";

	public static final String EQUALS_STR = "=";

	public static final String COMMA_STR = ",";

	public static final String EMPTY_STR = "";

	public static final String NEW_LINE = "\n";

	public static final String XML_HEADER_TAG = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";    

	/** ********************************************************************** */

	/***************************************************************************
	 * VTD-XML Parser Elements
	 **************************************************************************/
	private VTDGen vg = new VTDGen();

	private VTDNav vn = null;

	private AutoPilot ap = null;

	private XMLModifier xm = new XMLModifier();

	/** **********************************************************************/
	
	public XMLUtil(String xml) {
		try {
			vg = new VTDGen();
			vg.setDoc_BR(xml.getBytes());
			vg.parse(true);

			vn = vg.getNav();
			ap = new AutoPilot(vn);
			xm.bind(vn);
			vn.toElement(VTDNav.ROOT);
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}


	/**
	 * @param tagName
	 * @param attrName
	 * @return
	 * 
	 * Returns the value of the Required Atribute. Checks for the required
	 * attribute on all Occurence of the required XML Tag and adds each of them
	 * to a List.
	 * 
	 */
	public List<String> getAttrFromAllTags(String tagName, String attrName) {
		List<String> xmlAttrValues = new ArrayList<String>();
		try {
			vn.toElement(VTDNav.ROOT);
			ap.selectElement(tagName);
			while (ap.iterate()) {
				if (vn.getAttrVal(attrName) != -1) {
					xmlAttrValues.add(vn.toString(vn.getAttrVal(attrName)));
				}
			}

		} catch(Exception e){
			e.printStackTrace();
		}

		return xmlAttrValues;
	}
	
	/**
	 * @param tagName
	 * @return
	 * 
	 * Returns the Text Content of the required XML Tag. Checks for all
	 * occurence of the required XML Tag and returns the Text content as a List.
	 */
	public List<String> getTextFromAllTags(String tagName)  {
		List<String> xmlTextValues = new ArrayList<String>();
		try {
			vn.toElement(VTDNav.ROOT);
			ap.selectElement(tagName);
			while (ap.iterate()) {
				if (vn.getText() != -1)
					xmlTextValues.add(vn.toNormalizedString(vn.getText()));
				else
					xmlTextValues.add(null);
			}
		} catch(Exception e){
			e.printStackTrace();
		}

		return xmlTextValues;
	}
	
	public static String convertStreamToString(InputStream is) throws Exception {
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		StringBuilder sb = new StringBuilder();
		String line = null;
		while ((line = reader.readLine()) != null) {
			sb.append(line + "\n");
		}
		is.close();
		System.out.println(sb.toString());
		return sb.toString();
	}
}
